package com.zilla.android.lib.ui.setting;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.zilla.android.lib.ui.R;

import java.util.Objects;

/**
 * Created by zilla on 14-7-15.
 */
public class SettingItemLayout extends LinearLayout {

    private LayoutInflater mInflater;
    private Context mContext;
    private TextView titleTextView;
    private TextView desTextView;
    private ImageView imgView;

    public SettingItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SettingItemLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        mInflater.inflate(R.layout.ui_setting_layout_item, this, true);
//        this.setOrientation(LinearLayout.HORIZONTAL);
//        int padding = getResources().getDimensionPixelSize(R.dimen.border_padding);
//        this.setPadding(padding, padding, padding, padding);

        this.titleTextView = (TextView) findViewById(R.id.ui_setting_item_title);
        this.desTextView = (TextView) findViewById(R.id.ui_setting_item_des);
        this.imgView = (ImageView) findViewById(R.id.ui_setting_item_img);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemLayout);

        String titleStr = typedArray.getString(R.styleable.SettingItemLayout_settingItemLayout_title);
        String desStr = typedArray.getString(R.styleable.SettingItemLayout_settingItemLayout_des);
        int iconRes = typedArray.getResourceId(R.styleable.SettingItemLayout_settingItemLayout_img, R.drawable.setting_icon_enter);
        int bg = typedArray.getResourceId(R.styleable.SettingItemLayout_settingItemLayout_bg, R.drawable.transparent);

        if (!TextUtils.isEmpty(titleStr)) {
            titleTextView.setText(titleStr);
        }
        if (!TextUtils.isEmpty(desStr)) {
            desTextView.setText(desStr);
        }
        if (iconRes != R.drawable.transparent) {
            imgView.setImageResource(iconRes);
        }
        if (bg != R.drawable.transparent) {
            this.setBackgroundResource(bg);
            this.setTag(1);
        }

        typedArray.recycle();
    }

    public TextView getTitleTextView() {
        return titleTextView;
    }

    public void setTitleTextView(TextView titleTextView) {
        this.titleTextView = titleTextView;
    }

    public TextView getDesTextView() {
        return desTextView;
    }

    public void setDesTextView(TextView desTextView) {
        this.desTextView = desTextView;
    }

    public ImageView getImgView() {
        return imgView;
    }

    public void setImgView(ImageView imgView) {
        this.imgView = imgView;
    }
}